#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    : 2023/12/10 20:56
# @function: the script is used to do something.
# @version :

"""
音频文件地址放在了js代码里面
    不能使用css选择器获取对于的标签去获取数据了

re正则去匹配代码
    知道资源地址在哪里
"""

import re
import os
import requests

url = "https://houzi8.com/peiyue/qingyinyue/1.html"

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 '
                  'Safari/537.36',
}
"""
title:"良辰美景 古风柔和轻音乐",bpm:ex,duration:tS,version:b,cover_url:"\u002F\u002Fimg.houzi8.com\u002Faudio-cover\u002F2022-09-26\u002F4a990300b031665694c7559791e1fce4.jpg",preview_url:"\u002F\u002Fimg.houzi8.com\u002Faudio\u002Fpreview\u002F2023-12-06\u002F28e95e5d1954745be363a6dd67bd8e3e.mp3"
"""

response = requests.get(url=url, headers=headers)
# print(response.text)
result_list = re.findall('title:"(.*?)",.*?,preview_url:"(.*?)",', response.text)
# print(result_list)

save_path = './music/'

if not os.path.exists(save_path):
    os.mkdir(save_path)

for title, music_url in result_list:
    music_url = 'https:' + music_url.replace(r'\u002F', '/')
    # print(music_url)
    music_data = requests.get(music_url, headers=headers).content
    with open(save_path + title + '.mp3', 'wb') as f:
        f.write(music_data)
        print('下载成功', title)

